using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using Metro.Admin.BusinessEntities;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Transactions;

namespace Metro.Admin.DataAccess
{
    partial class AdminDataSource
    {
        public List<Credit> GetCredits()
        {
            List<Credit> credits = null;
            string id = string.Empty;
            string idCustomer = string.Empty;
            string type = string.Empty;
            DateTime date;
            decimal amount = 0;
            Customer customer = null;

            try
            {
                credits = new List<Credit>();
                string sqlCommand = "SELECT [id],[idCustomer],[type],[amount],[date] FROM [Credit]";
                                    
                DbCommand dbCommand = metroDb.GetSqlStringCommand(sqlCommand);
                using (IDataReader dataReader = metroDb.ExecuteReader(dbCommand))
                {
                    while (dataReader.Read())
                    {
                        id = dataReader["id"].ToString();
                        idCustomer = dataReader["idCustomer"].ToString();
                        type = dataReader["type"].ToString();
                        date = Convert.ToDateTime(dataReader["date"]);
                        amount = Convert.ToDecimal(dataReader["amount"]);
                        
                        customer = GetCustomer(idCustomer); 

                        credits.Add(new Credit(id,idCustomer,customer.Name,type,date,amount));
                    }
                }
            }
            catch(Exception ex)
            {
                throw ex;            
            }

            return credits;
        }
        public void CreateCredit(Credit credit)
        {
            Guid idCustomer = new Guid(credit.IdCustomer);
            
            try
            {
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
                {
                    string sqlCommand = "INSERT INTO [Credit] ([idCustomer],[type],[amount],[date]) VALUES (@idCustomer,@type,@amount,@date)";

                    DbCommand dbCommand = metroDb.GetSqlStringCommand(sqlCommand);

                    metroDb.AddInParameter(dbCommand, "@idCustomer", DbType.Guid, idCustomer);
                    metroDb.AddInParameter(dbCommand, "@type", DbType.String, credit.Type);
                    metroDb.AddInParameter(dbCommand, "@date", DbType.DateTime, DateTime.Now);
                    metroDb.AddInParameter(dbCommand, "@amount", DbType.Currency, credit.Amount);

                    metroDb.ExecuteNonQuery(dbCommand);
                    
                    if (credit.Type.Equals("C"))
                    {
                        sqlCommand = "UPDATE [Customer] SET [buyCredit] = @buyCredit WHERE idCustomer = @idCustomer";
                        
                        dbCommand = metroDb.GetSqlStringCommand(sqlCommand);
                        metroDb.AddInParameter(dbCommand, "@idCustomer", DbType.Guid, idCustomer);
                        metroDb.AddInParameter(dbCommand, "@buyCredit", DbType.Currency, credit.Amount);
                        metroDb.ExecuteNonQuery(dbCommand);
                    }
                    
                    if (credit.Type.Equals("V"))
                    {
                        sqlCommand = "UPDATE [Customer] SET [sellCredit] = @sellCredit WHERE idCustomer = @idCustomer";
                        dbCommand = metroDb.GetSqlStringCommand(sqlCommand);
                        metroDb.AddInParameter(dbCommand, "@idCustomer", DbType.Guid, idCustomer);
                        metroDb.AddInParameter(dbCommand, "@sellCredit", DbType.Currency, credit.Amount);
                        metroDb.ExecuteNonQuery(dbCommand);
                    }

                    if (credit.Type.Equals("N"))
                    {
                        sqlCommand = "UPDATE [Customer] SET [buyCredit] = @buyCredit,[sellCredit] = @sellCredit WHERE idCustomer = @idCustomer";
                        dbCommand = metroDb.GetSqlStringCommand(sqlCommand);
                        metroDb.AddInParameter(dbCommand, "@idCustomer", DbType.Guid, idCustomer);
                        metroDb.AddInParameter(dbCommand, "@buyCredit", DbType.Currency, credit.Amount);
                        metroDb.AddInParameter(dbCommand, "@sellCredit", DbType.Currency, credit.Amount);
                        metroDb.ExecuteNonQuery(dbCommand);
                    }
                    
                    scope.Complete();  
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void DeleteCredit(Credit credit)
        {
            Guid idCredit = new Guid(credit.Id);
            try
            {
                string sqlCommand = "DELETE FROM [Credit] WHERE id=@id";
                                    
                DbCommand dbCommand = metroDb.GetSqlStringCommand(sqlCommand);

                metroDb.AddInParameter(dbCommand, "@id", DbType.Guid, idCredit);
                metroDb.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
      
    }
}
